package com.jg.visitor.controller.module.admin;

import com.jg.visitor.common.bean.DoorSettingBean;
import com.jg.visitor.common.bean.PageDTO;
import com.jg.visitor.common.bean.ResponseBean;
import com.jg.visitor.common.bean.ServiceCode;
import com.jg.visitor.common.utils.StringUtils;
import com.jg.visitor.config.exception.JgException;
import com.jg.visitor.controller.dto.ApproverApproveRecordDTO;
import com.jg.visitor.controller.dto.BlacklistDTO;
import com.jg.visitor.controller.dto.UserDetailDTO;
import com.jg.visitor.controller.vo.*;
import com.jg.visitor.service.ApproverService;
import com.jg.visitor.service.BlacklistService;
import com.jg.visitor.service.GlobalConfigService;
import com.jg.visitor.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "管理员接口")
@RestController
@RequestMapping("/api/admin")
public class AdminController {

    @Resource
    private UserService userService;
    @Resource
    private GlobalConfigService globalConfigService;
    @Resource
    private ApproverService approverService;
    @Resource
    private BlacklistService blacklistService;

    @ApiOperation(value = "管理员分页查询账号", notes = "管理员分页查询账号")
    @PostMapping("/select-user-by-condition")
    public ResponseBean selectUserByCondition(@RequestBody AdminQueryVO adminQueryVO){
        PageDTO<UserDetailDTO> users = userService.adminSelectUserByCondition(adminQueryVO);
        return ResponseBean.success(users);
    }

    @ApiOperation(value = "管理员新增或编辑账号", notes = "新增或编辑账号")
    @PostMapping("/add-or-edit-account")
    public ResponseBean addOrEditAccount(@RequestBody AdminUserVO adminUserVO){

        userService.addOrEditAccount(adminUserVO);

        return ResponseBean.success();
    }

    @ApiOperation(value = "管理员删除用户账号", notes = "管理员删除用户账号")
    @DeleteMapping("/delete-account")
    public ResponseBean deleteAccount(Long userId){
        boolean flag = userService.deleteAccount(userId);

        return ResponseBean.success(flag);
    }

    @ApiOperation(value = "管理员设置访问时间", notes = "管理员设置访问时间")
    @GetMapping("/edit-max-visit-days")
    @ApiImplicitParams({@ApiImplicitParam(name = "days",value = "最大访问天数",dataTypeClass = String.class),
        @ApiImplicitParam(name = "beginTime",value = "能够访问的开始时间(HH:mm:ss)",dataTypeClass = String.class),
        @ApiImplicitParam(name = "endTime",value = "最迟访问的时间(HH:mm:ss)",dataTypeClass = String.class)})
    public ResponseBean editMaxVisitDays(String days,String beginTime,String endTime){

        boolean flag = globalConfigService.editVisitDayInfo(days,beginTime,endTime);

        return ResponseBean.success(flag);
    }

    @ApiOperation(value = "管理员查询大门设置", notes = "管理员查询大门设置")
    @GetMapping("/select-door-setting")
    public ResponseBean selectDoorSetting(){

        List<DoorSettingBean> result = globalConfigService.selectDoorSetting();

        return ResponseBean.success(result);
    }

    @ApiOperation(value = "管理员新增大门", notes = "管理员新增大门")
    @PutMapping("/add-door")
    public ResponseBean addDoor(String doorName){

        boolean flag = globalConfigService.addDoor(doorName);

        return ResponseBean.success(flag);
    }

    @ApiOperation(value = "管理员删除大门", notes = "管理员删除大门")
    @DeleteMapping("/delete-door")
    public ResponseBean deleteDoor(String doorId){

        boolean flag = globalConfigService.deleteDoor(doorId);

        return ResponseBean.success(flag);
    }

    @PostMapping("/select-record")
    @ApiOperation("管理员查询申请记录")
    public ResponseBean selectWaitSelfApproveRecord(@RequestBody AdminRecordQueryVO approverQueryVO){
        PageDTO<ApproverApproveRecordDTO> retList = approverService.selectWaitSelfApproveRecord(approverQueryVO);

        return ResponseBean.success(retList);
    }

    @ApiOperation("管理员查询核酸报告字段")
    @GetMapping("/select-nucleic-show")
    public ResponseBean selectNucleicShow(){
        String report = globalConfigService.selectNucleicShow();

        return ResponseBean.success(report);
    }

    @ApiOperation("管理员编辑核酸报告字段显示")
    @PutMapping("/select-nucleic-show")
    public ResponseBean editNucleicShow(@RequestParam("name") String name){
        if(StringUtils.isEmpty(name)){
            throw new JgException(ServiceCode.BAD_REQUEST);
        }

        globalConfigService.editNucleicShow(name);

        return ResponseBean.success();
    }

    @ApiOperation(value = "管理员查询黑名单列表", notes = "管理员查询黑名单列表")
    @PostMapping("/select-blacklist-by-condition")
    public ResponseBean selectBlacklistByCondition(@RequestBody BlackListQueryVO queryVO){
        PageDTO<BlacklistDTO> pageDTO = blacklistService.adminSelectBlacklistByCondition(queryVO);
        return ResponseBean.success(pageDTO);
    }

    @ApiOperation(value = "管理员把访客加入或者移除黑名单", notes = "管理员把访客加入黑名单")
    @PutMapping("/add-visitor-to-blacklist/{visitorId}/{operationType}")
    public ResponseBean addVisitorToBlacklist(@PathVariable Long visitorId, @PathVariable Integer operationType){
        boolean flag = blacklistService.addVisitorToBlacklist(visitorId,operationType);
        return ResponseBean.success(flag);
    }

    @ApiOperation(value = "管理员导入用户数据", notes = "管理员导入用户数据")
    @PostMapping("/upload-batch-user-template")
    public ResponseBean uploadBatchUser(@RequestPart("file") MultipartFile file){
        String msg = userService.uploadBatchUser(file);
        return ResponseBean.success(msg);
    }
}
